package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceVO;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * redis数据持久化
 *
 */
//@Component
public class RedisDataPersistenceJob {


    @Reference
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    //定时任务 指定某个方法 定时执行  cron 时间表达式
    @Scheduled(cron = "0/10 * * * * ?")
    public void doWord(){
        //攻略统计数据持久发
        //在redis中获取攻略所有数据vo对象
//        System.out.println("-----------开始持久化----------------");
//       List<StrategyStatisVO> list =  strategyStatisVORedisService.listStrategyStatisVo(RedisKeys.STRATEGY_STATIS_VO.getPrefix()+"*");
//        System.out.println("拿到的key值"+ RedisKeys.STRATEGY_STATIS_VO.getPrefix()+"*");
//        for (StrategyStatisVO vo : list) {
//            StrategyPersistenceVO pVO = new StrategyPersistenceVO();
//            BeanUtil.copyProperties(pVO,vo);
//            strategyDetailService.updateStratisVO(pVO);
//        }
//        System.out.println("-----------持久化结束----------------");
        System.out.println("-----------开始持久化----------------");

        List<StrategyDetail> list = strategyDetailService.list();
        System.out.println(list);
        for (StrategyDetail detail : list) {
            if (strategyStatisVORedisService.isZsetStatisVoExist(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(),detail.getId())){
                System.out.println("进到重复初始化1");
                continue;
            }

            //阅读+评论
            int score = detail.getReplynum() + detail.getViewnum();
            strategyStatisVORedisService.addHotScore(detail.getId(),score);
        }

        for (StrategyDetail detail : list) {
            if (strategyStatisVORedisService.isZsetStatisVoExist(RedisKeys.STRATEGY_STATIS_COMMEND_SORT.getPrefix(),detail.getId())){
                System.out.println("进到重复初始化2");
                continue;
            }
            //点赞+收藏
            int score = detail.getFavornum()+detail.getThumbsupnum();
            strategyStatisVORedisService.addCommendScore(detail.getId(),score);
        }

        System.out.println("-----------持久化结束----------------");

    }


}
